package com.saleSystem.controller;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.saleSystem.common.ServerResponse;
import com.saleSystem.pojo.User;
import com.saleSystem.service.IUserService;

/**
 * 用户控制
 * @author guanwei
 *
 */
@Controller("userController")
@RequestMapping("/")
public class UserController {

	@Autowired
	private IUserService iUserService;
	
	
	/**
	 * 登录页面
	 * @param session
	 * @param map
	 * @return
	 */
	@RequestMapping("login")
	public String login(HttpSession session,ModelMap map){
/*		User user = new User();
		user=(User) session.getAttribute("user");
		map.addAttribute("user", user);*/
		return "login";
	}
	
	/**
	 * 退出
	 * @param session
	 * @return
	 */
	@RequestMapping("logout")
	public String logout(HttpSession session){
		//���session
		session.invalidate();
		return "redirect:/login";
	}
	
	/**
	 * 登录
	 * @param userName
	 * @param password
	 * @param session
	 * @return
	 */
	@RequestMapping(value="api/login",method= RequestMethod.POST,produces="application/json;charset=UTF-8")
    @ResponseBody
    public ServerResponse<Boolean> login(@RequestParam("userName")String userName, @RequestParam("password")String password, 
    		HttpSession session){
		
		ServerResponse<Boolean> response = iUserService.userLogin(userName, password);
		
		if(response.getResult()){
			User user = iUserService.getUser(userName);
			session.setAttribute("user", user);
		}
		
        return response;
    }
	
}
